<template>
	<view class="box">
		<view class="hezi">
			<view class="toux">
				<view class="ts">头像</view>
				<view class="tou_flex">
					<image v-if="userInfo.headImg" :src="userInfo.headImg" mode=""></image>
					<image v-else src="../../static/login.png" mode=""></image>
					<uni-icons type="right" size="20"></uni-icons>
				</view>
				<button :plain="true" class="btn_ads" open-type="chooseAvatar" @chooseavatar='chooseavatar'></button>
			</view>
			<view class="toux">
				<view class="ts">昵称</view>
				<view class="tou_flex">
					<input v-model="userInfo.nickName" type="nickname" placeholder="请输入昵称" @change="getNickname"/>
					<uni-icons type="right" size="20"></uni-icons>
				</view>
			</view>
			<view class="toux" style="border-bottom: none;">
				<view class="ts">手机号</view>
				<view class="tou_flex">
					<view class="ts1">
						{{userInfo.phone}}
					</view>
				</view>
			</view>
		</view>

		<view class="btn">
			<view class="btns" @click="handleBc">
				保存
			</view>
			<view class="btns b1" @click="handleLoginOut">
				退出登录
			</view>
		</view>

	</view>
</template>

<script>
	import {
		getuserInfo,setuserInfo
	} from '@/common/http.api.js';
	export default {
		data() {
			return {
				userInfo: {
				},
			}
		},
		onLoad() {
			getuserInfo().then(res => {
				if (res.code == 200) {
					this.userInfo = res.data
				}
			})
		},
		methods: {
			getNickname(e){
				this.userInfo.nickName = e.detail.value
			},
			handleBc(){
				setuserInfo(this.userInfo).then(res=>{
					if(res.code == 200){
						uni.showToast({
							icon:'success',
							title:'修改成功'
						})
						getuserInfo().then(res => {
							if (res.code == 200) {
								this.userInfo = res.data
							}
						})
					}
				})
			},
			handleLoginOut() {
				uni.reLaunch({
					url: '/pages/my/login'
				})
				uni.clearStorage()
			},
			chooseavatar(e) {
				let that = this
				uni.uploadFile({
					url: `https://javatest.heiwangke.net/fine-resource-api/common/upload`, // 后台接口
					filePath: e.detail.avatarUrl, // 上传图片 url
					name: 'file',
					header: {
						'Content-Type': 'multipart/form-data',
						'Authorization':uni.getStorageSync('token')
					},
					success(res) {
						let img = JSON.parse(res.data).url
						that.userInfo.headImg = img
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F0F0F1;
	}

	.hezi {
		width: 716rpx;
		background: #FFF;
		border-radius: 14rpx;
		padding: 20rpx;
		box-sizing: border-box;
		margin: 20rpx auto;
	}

	.toux {
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1px #EEEEEE solid;
		height: 120rpx;
		position: relative;

		.btn_ads {
			position: absolute;
			right: 0%;
			width: 140rpx;
			height: 100rpx;
			border: none;
		}

		.ts {
			font-family: PingFang SC;
			font-weight: bold;
			font-size: 28rpx;
			color: #333333;
		}

		.ts1 {
			font-family: PingFang SC;
			font-size: 28rpx;
			color: #333333;
		}

		image {
			width: 98.1rpx;
			height: 98.1rpx;
			border-radius: 50%;
		}

		.tou_flex {
			display: flex;
			align-items: center;

			input {
				font-size: 28rpx;
				text-align: right;
			}
		}
	}

	.btn {
		width: 716rpx;
		margin: 80rpx auto;

		.btns {
			width: 716rpx;
			height: 88rpx;
			border-radius: 10rpx;
			text-align: center;
			line-height: 88rpx;
			font-size: 30rpx;
			background-color: #022B5F;
			color: #FFF;
		}

		.b1 {
			background-color: #fff;
			margin-top: 40rpx;
			color: #333333;
		}
	}
</style>